<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once(OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php');
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfForm.php';

$aForms = getForms();

if (!isset($aForms[reqVar('cod')])) {
	backOnErr("formView - manca cod");
	exit();
}

reqDef('campo_obb','N');
reqDef('campo_readonly','N');
$aFormX = $aForms[reqVar('cod')];

if (reqVar('azione') == 'salva') {
	$msgPag = confIns();
} 
$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:submAz('salva');\" >salva</a>";
$sCmdR .= cmdTorna('torna');

$aStili[''] = 'normale';
$aStili['b'] = 'grassetto';

$aTypes['stringa'] = 'stringa';
$aTypes['numero'] = 'numero';
$aTypes['data'] = 'data';
$aTypes['bool'] = 'Si\'/No';
$aTypes['nota'] = 'nota';
$aTypes['tabella'] = 'tabella';

$aCampi = array();
foreach($aFormX['files'] as $nomeX => $aX) {
	$aCampiDef = getDefCampi($nomeX);
	foreach($aCampiDef as $campoY => $aY) {
		$aCampoZ = $aX['alias']."_".$campoY;
		$aCampi[$aCampoZ] = $aCampoZ;
	}
}
$aCampi['=calc='] = _t('= calcolato =');

testaHtml('Gestione form', 'Nuovo campo',$msgPag, 'gest');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<?php 
echo getHtmlForm();
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('cod');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
 			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?php echo getHtmlRigaRif(20);?>
   <tr>
      <td class="lab1" colspan='3' align='left'>Form</td>
      <td class="txt1" colspan='3' align='left'>
			<span style='font-family:lucida console,courier;'><?=reqVar('cod')?></span>
		</td>
      <td class="txt1" colspan='14' align='left'>
			<?=$aFormX['descr']?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3'>File</td>
      <td class="txt1" colspan='17' align='left'>
      	<?
			foreach($aFormX['files'] as $nomeX => $aX) {
				echo $aX['alias']." - ".$nomeX."<br/>";
			}	
			?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3'>Riga</td>
		<td class="txt1" colspan='2'>
			<?=getHtmlText('riga',3);?>
		</td>
		<td class="lab1" colspan='3'>Posizione</td>
		<td class="txt1" colspan='12'>
			<?=getHtmlText('posiz',3);?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3'>Etichetta</td>
		<td class="txt1" colspan='5'>
			<?=getHtmlText('label',30);?>
		</td>
		<td class="lab1" colspan='3'>Dimensione</td>
		<td class="txt1" colspan='2'>
			<?=getHtmlText('label_size',3);?>
		</td>
		<td class="lab1" colspan='2'>Stile</td>
		<td class="txt1" colspan='5'>
			<?=getHtmlSelect('label_style',$aStili);?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3'>Campo</td>
		<td class="txt1" colspan='5'>
			<?=getHtmlSelect('campo',$aCampi);?>
		</td>
		<td class="lab1" colspan='3'>Dimensione</td>
		<td class="txt1" colspan='2'>
			<?=getHtmlText('campo_size',3);?>
		</td>
		<td class="lab1" colspan='2'>Stile</td>
		<td class="txt1" colspan='5'>
			<?=getHtmlSelect('campo_style',$aStili);?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3'>Campo calc.</td>
		<td class="txt1" colspan='5'>
			<?
			echo getHtmlText('campo_calc',25);
			?>
		</td>
		<td class="lab1" colspan='3'>Modificabile</td>
		<td class="txt1" colspan='12'>
			<?=getHtmlRadio('campo_readonly',array('N'=>'Si\'','S'=>'No'));?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3'>Obbligatorio</td>
		<td class="txt1" colspan='5'>
			<?=getHtmlRadio('campo_obb',array('S'=>'Si\'','N'=>'No'));?>
		</td>
		<td class="lab1" colspan='3'>Tipo</td>
		<td class="txt1" colspan='3'>
			<?=getHtmlSelect('campo_type',$aTypes);?>
		</td>
		<td class="lab1" colspan='2'>Dec./righe(*)</td>
		<td class="txt1" colspan='4'>
			<?=getHtmlText('campo_dec',2);?>
		</td>
   </tr>
   <tr>
		<td class="lab1" colspan='3'>Tabella</td>
		<td class="txt1" colspan='5'>
			<?
			$aTabelle = array(''=>'-- seleziona una tabella--');
			$sSql = "SELECT t.*" .
						" FROM ovf_tabelle t " .
						" ORDER BY t.descr ";
			$objDB->query($sSql,'selTab');
			while($acX=$objDB->fetch('selTab')) {
				$aTabelle[$acX['cod_tabella']] = $acX['descr'];
			}
			$aOptTab = array("onChange"=>
				"document.frm1.campo_tab_k.selectedIndex=0;document.frm1.campo_tab_v.selectedIndex=0;submAz('reload');");
			echo getHtmlSelect('campo_tabella',$aTabelle, '@REQ', $aOptTab);?>
		</td>
		<td class="lab1" colspan='2'>Chiave</td>
		<td class="txt1" colspan='4'>
			<?php 
			$aCampiTab = array(''=>'-- seleziona un campo --');
			if (reqVar('campo_tabella')!="") {
				$sSql = "SELECT t.*" .
							" FROM ovf_tabelle t " .
							" WHERE cod_tabella = '".reqVarAS('campo_tabella')."'";
				$objDB->query($sSql,'selTab');
				$acX=$objDB->fetch('selTab');
				$aZ = explode(',',$acX['lista_campi']);
				foreach($aZ as $kY => $sY) {
					if (trim($sY)!='') {
						$aCampiTab[$kY] = trim($sY);
					}
				}				
			}
			echo getHtmlSelect('campo_tab_k',$aCampiTab);?>
		</td>
		<td class="lab1" colspan='2'>Valore</td>
		<td class="txt1" colspan='4'>
			<?php echo getHtmlSelect('campo_tab_v',$aCampiTab);?>
		</td>
   </tr>
   <tr>
		<td class="txt1" colspan='20'>(*) numero decimali se tipo = numero
		, numero righe campo modifica se tipo = nota
   </tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
 			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
			<?
echo "<form/>\n";
codaHtml();
exit();
function confIns() {
	global $objDB;
	$msg = '';
	
	if (reqVarStr2num('riga')===false || reqVarStr2num('riga')==0) {
		$msg .= "riga errata<br>";
	}
	if (reqVarStr2num('posiz')===false || reqVarStr2num('posiz')==0) {
		$msg .= "posizione errata<br>";
	}
	if (reqVarStr2num('label_size')===false) {
		$msg .= "dim. etich. errata<br>";
	}
	if (reqVarStr2num('campo_dec')===false) {
		$msg .= "decimali campo errati<br>";
	}
	if (reqVarStr2num('campo_size')===false) {
		$msg .= "dim. campo errata<br>";
	}
	if (reqVarStr2num('label_size')==0 && reqVarStr2num('campo_size')==0) {
		$msg .= "dim. entrambi a zero<br>";
	}
	if ((reqVarStr2num('label_size')+reqVarStr2num('campo_size'))>20) {
		$msg .= "somma dim. > 20<br>";
	}
	if (reqVar('campo_type')=='tabella' && reqVar('campo_tabella')=='') {
		$msg .= "selezionare una tabella<br>";
	}
	if (reqVar('campo_type')=='tabella' && reqVar('campo_tab_k')=='') {
		$msg .= "selezionare una chiave tabella<br>";
	}
	if (reqVar('campo_type')=='tabella' && reqVar('campo_tab_v')=='') {
		$msg .= "selezionare un valore tabella<br>";
	}
	$_REQUEST["campo_calc"] = strtolower(reqVarTrim("campo_calc"));
	if (preg_match("/[^a-z0-9_/",reqVarTrim("campo_calc"))) {
		$msg .= "campo calcolato - nome errato (lettere, numeri e _)<br>";
	}
	if (reqVar("campo")=='=calc=' && reqVarTrim("campo_calc")=='') {
		$msg .= "campo calcolato - manca nome<br>";
	}
	if (reqVar("campo")!='=calc=' && reqVarTrim("campo_calc")!='') {
		$msg .= "campo non calcolato - no nome<br>";
	}
	$oForm = new ovfForm(reqvar('cod'));
	//dbg_($oForm);
	if (isset($oForm->aCampiDBDef[reqVar('campo')])) {
		switch (substr($oForm->aCampiDBDef[reqVar('campo')][0],0,4)) {
			case 'date':
				if (reqVar('campo_type')!='data') {
					$msg .= "incongruenza tipo data - campo<br>";
				}
				break;
			case 'int(':
			case 'deci':
				if (reqVar('campo_type')!='numero') {
					$msg .= "incongruenza tipo numero - campo<br>";
				}
				break;
			case 'text':
				if (reqVar('campo_type')!='nota') {
					$msg .= "incongruenza tipo ".reqVar('campo_type')
						." - campo ".$oForm->aCampiDBDef[reqVar('campo')][0]."<br>";
				}
				if (reqVar('campo_dec')<1) {
					$msg .= "n.righe deve essere > 0<br>";
				}
				break;
			case 'char':
			case 'varc':
			default:
				if (reqVar('campo_type')!='stringa'
				&& reqVar('campo_type')!='bool'
				&& reqVar('campo_type')!='tabella') {
					$msg .= "incongruenza tipo ".reqVar('campo_type')." - campo<br>";
				}
				break;
		}
	}
	
	$sqlSel = "SELECT * FROM ovf_campi_form " .
			" WHERE cod_form = '" .intval(reqvar('cod'))."'".
			" AND riga = ".intval(reqVar('riga'))." AND posiz = ".intval(reqVar('posiz'));
	$objDB->exec($sqlSel);
	$aRow = $objDB->fetch();
	if ($aRow) {
		$msg .= "riga/posizione gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	if (reqVar('scaduto')!='S') $_REQUEST['scaduto'] = 'N';
	$campo = (reqVar("campo")=='=calc=')?"W_".reqVarAS("campo_calc"):reqVarAS("campo");
	$sSqlIns = "INSERT INTO ovf_campi_form SET cod_form = '" .reqVar('cod')."'".
			", riga = ".intval(reqVar('riga')).
			", posiz = ".intval(reqVar('posiz')).
			", label = '".reqVarAS('label')."'".
			", label_style = '".reqVarAS('label_style')."'".
			", label_size = ".intval(reqVar('label_size')).
			", campo = '".$campo."'".
			", campo_readonly = '".reqVarAS('campo_readonly')."'".
			", campo_obb = '".reqVarAS('campo_obb')."'".
			", campo_type = '".reqVarAS('campo_type')."'".
			", campo_dec = ".reqVarInt('campo_dec').
			", campo_size = ".reqVarInt('campo_size').
			", campo_style = '".reqVarAS('campo_style')."'".
			", campo_tab = '".reqVarAS('campo_tabella')."@".reqVarAS('campo_tab_k')."@".reqVarAS('campo_tab_v')."'";
		$objDB->exec($sSqlIns);
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: formView.php?cod=".reqvar('cod').
		"&msgpag=".urlencode('campo inserito')."&xsn=".XSN);
	exit();
}
?>